package com.bestcem.xm.ticket.controller.v2;

import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.component.security.dto.TokenDTO;
import com.bestcem.xm.ticket.controller.v2.param.WeChatTicketSearchParam;
import com.bestcem.xm.ticket.controller.vo.ticket.TicketPageVO;
import com.bestcem.xm.ticket.service.WechatWorkService;
import com.bestcem.xm.ticket.service.dto.PageDTO;
import com.bestcem.xm.ticket.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 企业微信小程序定制接口
 *
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version WechatWork
 * @date 2021/12/17 15:12
 */
@Slf4j
@RestController(value = "TicketV2WechatWorkController")
@RequestMapping(value = "/ticket/v2/wechat", produces = MediaType.APPLICATION_JSON_VALUE)
public class TicketWechatWorkController {

    @Resource
    private HttpServletRequest request;

    @Resource
    private WechatWorkService wechatWorkService;


    /**
     * 企业微信小程序定制版 工单分页搜索接口
     *
     * @date 2021/12/17 15:28
     * @author XiaoYunSong <ys.xiao@idiaoyan.com>
     */
    @PostMapping(value = "/ticket/search")
    public XmResultJson<PageDTO<TicketPageVO>> search(@RequestBody WeChatTicketSearchParam param) {
        if (Objects.isNull(param.getPage())) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少page参数");
        }
        if (Objects.isNull(param.getSize())) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_LOST, "缺少rowsPerPage参数");
        }
        if (param.getPage() <= 0) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "page不在范围内");
        }
        if (param.getSize() <= 0 || param.getSize() >= 999) {
            return XmResultJson.fail(ReqErrorCodeEnum.PARAM_RANGE, "rowsPerPage不在范围内");
        }
        // 前端传来的page至少为1,因此这里减去1
        param.setPage(param.getPage() - 1);

        // 替换搜索输入里面的特殊字符
        if (!ObjectUtils.isEmpty(param.getSearch())) {
            param.setSearch(CommonUtils.escapeRegexChar(param.getSearch()));
        }
        
        
        
        String orgId = SecurityContextHolder.getOrgId();
        String userId = SecurityContextHolder.getUserId();
        TokenDTO token = SecurityContextHolder.getToken();
        boolean isSuper = false;
        if (Objects.nonNull(token)) {
            isSuper = token.getSuperRole() == 1;
        }

        param.setOrgId(orgId);
        param.setUserId(userId);
        param.setSuper(isSuper);

        ServiceResult<PageDTO<TicketPageVO>> result = wechatWorkService.searchTicket(param);
        if (!result.isSuccess()) {
            return XmResultJson.fail(result.getErrorData(), result.getMsg());
        }
        return XmResultJson.success(result.getData());
    }


}
    